package ru.kruzhki.client.ui.searchresults;

import java.util.List;

import ru.kruzhki.client.gwt.Maskable;
import ru.kruzhki.client.gwt.Maskables;
import ru.kruzhki.client.gwtp.AbstractView;
import ru.kruzhki.shared.model.Company;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.inject.Inject;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.widget.core.client.ListView;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;

public class SearchResultsView extends AbstractView<SimpleContainer> implements SearchResultsPresenter.MyView {
	  interface Template extends SafeHtmlTemplates {
		    @Template("<b>{0}</b>")
		    SafeHtml img(String url);
		  }

		  private static Template template;

	private static String html = "<h1>Remote Procedure Call</h1>\n"
			+ "<table align=\"center\">\n" + "  <tr>\n"
			+ "    <td style=\"font-weight:bold;\">Sending name to server:</td>\n"
			+ "  </tr>\n" + "  <tr>\n"
			+ "    <td id=\"textToServerContainer\"></td>\n" + "  </tr>\n"
			+ "  <tr>\n"
			+ "    <td style=\"font-weight:bold;\">Server replies:</td>\n"
			+ "  </tr>\n" + "  <tr>\n"
			+ "    <td id=\"serverResponseContainer\"></td>\n" + "  </tr>\n"
			+ "  <tr>\n" + "    <td id=\"closeButton\"></td>\n" + "  </tr>\n"
			+ "</table>\n";

	private Label textToServerLabel;
	private HTML serverResponseLabel;

	private Maskable maskable;

	private ListView<Company, String> listView;


	@Inject
	public SearchResultsView() {
	    if (template == null) {
		      template = GWT.create(Template.class);
		    }
	}

	@Override
	protected SimpleContainer createWidget() {
		textToServerLabel = new Label();
		serverResponseLabel = new HTML();
		
		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		HTMLPanel panel = new HTMLPanel(html);
		panel.add(textToServerLabel, "textToServerContainer");
		panel.add(serverResponseLabel, "serverResponseContainer");
		
		SimpleContainer simplePanel=new SimpleContainer();
		simplePanel.setWidget(panel);
		simplePanel.setBorders(true);
		ModelKeyProvider<Company> keyProvider=new ModelKeyProvider<Company>() {
			@Override
			public String getKey(Company item) {
				return String.valueOf(item.getId());
			}
		};

		ListStore<Company> store=new ListStore<Company>(keyProvider);
		ValueProvider<Company, String> valueProvider=new ValueProvider<Company, String>() {
			
			@Override
			public void setValue(Company object, String value) {
				//readonly
			}
			
			@Override
			public String getValue(Company object) {
				return object.toString();
			}
			
			@Override
			public String getPath() {
				// TODO Auto-generated method stub
				return null;
			}
		};
		Cell<String> cell=new AbstractCell<String>() {
			  @Override
			  public void render(Context context, String value, SafeHtmlBuilder sb) {
			    if (value != null) {
			      // The template will sanitize the URI.
			      sb.append(template.img(value));
			    }
			  }
			}
		;
		listView = new ListView<Company, String>(store, valueProvider, cell);
		listView.getSelectionModel().addSelectionHandler(new SelectionHandler<Company>() {
			
			@Override
			public void onSelection(SelectionEvent<Company> event) {
				new MessageBox(event.getSelectedItem().toString()).show();
			}
		});
		simplePanel.setWidget(listView);
		return simplePanel;
	}

	/* (non-Javadoc)
	 * @see ru.kruzhki.client.ui.searchresults.SearchResultsPresenter.MyView#setSearchResults(java.util.List)
	 */
	@Override
	public void setSearchResults(List<Company> companies) {
		listView.getStore().clear();
		listView.getStore().addAll(companies);
	}
	/* (non-Javadoc)
	 * @see ru.kruzhki.client.gwtp.AbstractView#getMaskable()
	 */
	@Override
	protected Maskable getMaskable() {
		if(maskable==null){
			maskable=Maskables.newMaskable(asWidget());
		}
		return maskable;
	}
	
}
